package com.jeasonzhao.report.dataset;

import java.io.Serializable;

import com.jeasonzhao.commons.basic.StringCollection;
import com.jeasonzhao.commons.json.JSONClass;
import com.jeasonzhao.commons.json.JSONNode;
import com.jeasonzhao.commons.utils.Algorithms;
import com.jeasonzhao.commons.utils.DataTypes;
import com.jeasonzhao.report.codes.BooleanOperations;
import com.jeasonzhao.report.model.ObjectCollection;
import com.jeasonzhao.report.model.ReportParam;

public class DataSetFilter implements Serializable
{
    private static final long serialVersionUID = 1L;
    private String m_strFilterId = null;
    private String m_strColGuid = null;
    private BooleanOperations m_BooleanOperations = BooleanOperations.Equals;
    private ObjectCollection m_Values = null;
    private StringCollection m_ValueNames = null;
    public DataSetFilter()
    {
    }

    public String getFilterId()
    {
        return m_strFilterId;
    }

    public void setFilterId(String obj)
    {
        m_strFilterId = obj;
    }

    public String getColGuidId()
    {
        return m_strColGuid;
    }

    public void setColGuidId(String obj)
    {
        m_strColGuid = obj;
    }

    public BooleanOperations getBooleanOperation()
    {
        return m_BooleanOperations == null ? BooleanOperations.Equals : m_BooleanOperations;
    }

    public void setBooleanOperation(BooleanOperations obj)
    {
        m_BooleanOperations = obj;
    }

    public ObjectCollection getValues()
    {
        return m_Values;
    }

    public void setValues(ObjectCollection obj)
    {
        m_Values = obj;
    }

    public StringCollection getValueNames()
    {
        return m_ValueNames;
    }

    public void setValueNames(StringCollection obj)
    {
        m_ValueNames = obj;
    }

    public JSONNode toJSON(DataTypes dataType)
    {
        StringBuffer strValues = new StringBuffer();
        int nc = 0;
        for(int n = 0;null != this.getValues() && n < this.getValues().size();n++)
        {
            if(null == this.getValues().get(n))
            {
                continue;
            }
            Object obj = Algorithms.castValueSafe(dataType,this.getValues().get(n));
            if(null == obj)
            {
                continue;
            }
            String str = ReportParam.getFormatedValueString(obj);
            if(str == null || str.trim().length() < 1)
            {
                continue;
            }
            if(nc > 0)
            {
                strValues.append(',');
            }
            nc++;
            strValues.append(str);
        }
        String strValuesName = this.getValueNames() == null ? "" :
            (this.getValueNames().size() > 1 ?
             "[" + this.getValueNames().toString(",",false) + "]" :
             this.getValueNames().toString(",",false)
            );
        JSONClass cls = new JSONClass();
        cls.set("filterId",this.getFilterId())
            .set("colGuid",this.getColGuidId())
            .set("operation",this.getBooleanOperation().getConstantFieldName())
            .set("value",strValues.toString())
            .set("valueName",strValuesName)
            ;
        return cls;
    }

    public static DataSetFilter fromJSON(JSONClass cls)
    {
        if(null == cls)
        {
            return null;
        }
        DataSetFilter col = new DataSetFilter();
        col.setFilterId(cls.valueOf("filterId"));
        col.setColGuidId(cls.valueOf("colGuid"));
        col.setBooleanOperation(BooleanOperations.fromName(cls.valueOf("operation")));
//        System.out.println(col.getBooleanOperation()+"==="+cls.getAttributeValue("operation"));
        String strValues = cls.valueOf("value");
        if(null != strValues)
        {
            if(strValues.startsWith("["))
            {
                strValues = strValues.substring(1);
            }
            if(strValues.endsWith("]"))
            {
                strValues = strValues.substring(0,strValues.length() - 1);
            }
            col.setValues(new ObjectCollection((Object[]) strValues.split(",")));
        }
        strValues = cls.valueOf("valueName");
        if(null != strValues)
        {
            if(strValues.startsWith("["))
            {
                strValues = strValues.substring(1);
            }
            if(strValues.endsWith("]"))
            {
                strValues = strValues.substring(0,strValues.length() - 1);
            }
            col.setValueNames(StringCollection.splitString(strValues,","));
        }
        return col;
    }

}
